<template>
  <!-- 栅格系统：布局 -->
  <el-row style="width:100%">
    <el-col :span="20">{{ treeNode.name }}</el-col>
    <el-col :span="4">
      <el-row>
        <el-col :span="12">{{ treeNode.manager }}</el-col>
        <el-col :span="12">
          <!-- 3.下拉菜单 -->
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit"
                >编辑部门</el-dropdown-item
              >
              <el-dropdown-item v-if="!isRoot" command="del"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  // 父传子数据:
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      // 设置它的默认值
      default: false
    }
  },
  methods: {
    handleCommand (command) {
      if (command == 'add') {
        //
        this.$emit('addDep', this.treeNode.id)
      } else if (command == 'edit') {
        // 点击编辑部门：
        this.$emit('editDept', this.treeNode.id)
      } else if (command == 'del') {
        // 点击删除，弹出提示框
        this.$confirm('是否删除该部门？', '温馨提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        })
          .then(async () => {
            // 点击确定删除：发请求删除、提示，重新请求新数据。
            await delDepartments(this.treeNode.id)
            this.$message.success(this.treeNode.name + '成功删除！')
            // 重新请求组织架构数据，加载父子间页面
            // 注意;重新发出自定义事件（子事件通知父）！！
            this.$emit('delpet')
          })
          .catch(() => {
            this.$message('已取消删除')
          })
      }
    }
  }
}
</script>

<style></style>
